#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QVBoxLayout *mainLayout=new QVBoxLayout(ui->centralwidget);
    QPushButton *pbAddItme=new QPushButton("add root");
    //QPushButton *pbAddnewSubNode=new QPushButton("add subnode");
    connect(pbAddItme,&QPushButton::clicked,this,&MainWindow::slots_addItem);
    //connect(pbAddnewSubNode,&QPushButton::clicked,this,&MainWindow::slots_addSubnode);

    m_treeWidget=new QTreeWidget(this);
    m_treeWidget->setColumnCount(4);
    m_treeWidget->setColumnWidth(1,50);
    m_treeWidget->setColumnWidth(2,50);
    m_treeWidget->setColumnWidth(3,50);
    m_treeWidget->header()->setSectionResizeMode(1,QHeaderView::Fixed);
    m_treeWidget->header()->setSectionResizeMode(2,QHeaderView::Fixed);
    m_treeWidget->header()->setSectionResizeMode(3,QHeaderView::Fixed);
    m_treeWidget->header()->setSectionResizeMode(0,QHeaderView::Stretch);
    m_treeWidget->header()->setStretchLastSection(false);

    m_treeWidget->header()->setVisible(false);
    connect(m_treeWidget,&QTreeWidget::itemDoubleClicked,this,&MainWindow::slots_openfile);
    mainLayout->addWidget(pbAddItme);
    //mainLayout->addWidget(pbAddnewSubNode);
    mainLayout->addWidget(m_treeWidget);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slots_addItem()
{
    QPushButton* btn_del = new QPushButton("del");
    connect(btn_del,&QPushButton::clicked,this,&MainWindow::slots_deleteItem);
    QPushButton* btn_add = new QPushButton("add");
    connect(btn_add,&QPushButton::clicked,this,&MainWindow::slots_addSubnode);
    QTreeWidgetItem* item = new QTreeWidgetItem(QStringList() << "asdfasdf");
    item->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::
                     ItemIsSelectable);
    //item->setCheckState(0, Qt::Unchecked);
    item->setData(0,Qt::UserRole+1,"root");
    m_treeWidget->addTopLevelItem(item);
    m_treeWidget->setItemWidget(item,1,btn_add);
    m_treeWidget->setItemWidget(item,2,btn_del);

}

void MainWindow::slots_addSubnode()
{
    QTreeWidgetItem * currentItem= m_treeWidget->currentItem();
    if(currentItem!=NULL)
    {
        QPushButton* btn_del = new QPushButton("del");
        connect(btn_del,&QPushButton::clicked,this,&MainWindow::slots_deleteItem);
        QTreeWidgetItem * item=new QTreeWidgetItem(QStringList()<<"1234567");
        item->setFlags(Qt::ItemIsUserCheckable|Qt::ItemIsEnabled|Qt::
                             ItemIsSelectable);
            //item->setCheckState(0, Qt::Unchecked);
            item->setData(0,Qt::UserRole+1,"sub node");
            currentItem->addChild(item);
            m_treeWidget->setItemWidget(item,1,btn_del);
    }
    else
    {
        QMessageBox::information(this,"提示","请选择父节点");
    }

}

void MainWindow::slots_deleteItem()
{
    QTreeWidgetItem * currentItem= m_treeWidget->currentItem();
    if(currentItem!=NULL)
    {
        qDebug()<<"name:"<<currentItem->data(0,Qt::UserRole+1);
        if(currentItem->parent()==NULL)
        {
            delete m_treeWidget->takeTopLevelItem(m_treeWidget->currentIndex().row());

        }
        else
        {
            delete currentItem->parent()->takeChild(m_treeWidget->currentIndex().row());

        }
    }
}

void MainWindow::slots_openfile(QTreeWidgetItem* indexItem, int itemID)
{
    QString itemText = indexItem->text(itemID);
    qDebug()<<"open file:"<<indexItem->data(0,Qt::UserRole+1).toString()<<";item text:"<<itemText;
}

